<template>
  <div>
    <!-- 索赔结算单查询-详情页面 -->
    <BreakSpace label="结算单信息" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel ref="formPanel" :list="formList" :cols="3" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div style="padding-top: 3rem;"></div>
    <BreakSpace label="结算信息" :containerStyle="{ marginBottom: '10px' }" />
    <div v-show="this.rowData.finalStatementType == '50021001'">
    <FilterTable
      ref="table2"
      :isToperInfo = false
      :columns="columns2"
      :params="fetchParams"
      :dataSource="dataList"
      :datakey="'data'"
      :onColumnsChange="columns2 => (this.columns2 = columns2)"
      :onRowSelectChange="selectChange"
      :isPagination = false
      :isSelectColumn="false"
      height="auto"
      :is-select-column= false
    >
    </FilterTable>
    </div>

    <FilterTable
      ref="table3"
      :isToperInfo = false
      :columns="columns3"
      :params="fetchParams"
      :dataSource="dataList"
      :datakey="'items'"
      :onColumnsChange="columns3 => (this.columns3 = columns3)"
      :onRowSelectChange="selectChange"
      :isPagination = false
      :isSelectColumn="false"
      height="auto"
      :is-select-column= false
    >
    </FilterTable>
    <BreakSpace label="索赔单列表" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable
      ref="table"
      :dataSource="dataList"
      :columns="columns"
      :params="fetchParams"
      :datakey="'data'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :isPagination = false
      :isSelectColumn="false"
      height="auto"
      :is-select-column= false
    >
    </FilterTable>
    <BreakSpace label="抵扣单列表" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable
      ref="withholdTable"
      :dataSource="dataList"
      :columns="withholdColumns"
      :params="fetchParams"
      :datakey="'data'"
      :onColumnsChange="columns => (this.withholdColumns = withholdColumns)"
      :onRowSelectChange="selectChange"
      :isPagination = false
      :isSelectColumn="false"
      height="auto"
      :is-select-column= false
    >
    </FilterTable>
    <!-- <div style="padding-top: 3rem;"></div>
    <BaseDialog :visible.sync="suopeidanFlag" :title="'索赔单选择'" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <Selectsuopeidan @close="suopeidanclose" @save="suopeidansave"></Selectsuopeidan>
    </BaseDialog> -->
    <BasePrint ref="templateReturnDetails" :data="printList" template="claim/templateSettleBils"/>

      <!-- <AjaxButton size="small" type="primary" icon="el-icon-download" :click="ownerExportData">打印结算通知单</AjaxButton> -->
      <multiuse-button size="small" type="primary" @click="batchExamineDetails">打印索赔单</multiuse-button>
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    <!-- <BreakSpace label="考核单列表" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable
      ref="table1"
      :columns="columns1"
      :params="fetchParams"
      :datakey="'items'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :isPagination = false
      :isSelectColumn="false"
      height="auto"
      :is-select-column= false
    >
    </FilterTable>
    <div style="padding-top: 3rem;"></div>
    <BaseDialog :visible.sync="kaohedanFlag" :title="'考核单选择'" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <SelectKaoHedan @close="kaohedanclose" @save="kaohedansave"></SelectKaoHedan>
    </BaseDialog>
    <div style="padding-top: 3rem;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    </div>
    <BaseDialog :title="'查看附件'" :visible.sync="upVisible" destroyOnClose>
      <uploadFile :rowData="assessId" @close="upVisible = false"></uploadFile>
    </BaseDialog> -->
  </div>
</template>
<script>

  import { isFormEmpty } from '@/utils/index';
  import { getCloseAccountCount, getPartType,getClaimInfoByNo,getCostCountByNo } from '@/api/claim/dealer/ClaimStatementQueryDealer';
  // import { approve,quyrpBatch} from '@/api/claim/factory/ClaimInvoiceCheck';
  import { dictionary } from '@/utils/dictMixin';
  import uploadFile from "@/pages/claimDemage/BasicData/ClaimInvoiceCheck/uploadFile";
  export default {
    mixins: [dictionary],
    props: ['rowData'],
    components: {
      uploadFile
    },
    data() {
      return {
        dataList: [],
        dataList1: [],
        kaohesum: 0,
        suopeisum: 0,
        huanxiajiandataList: [],
        fujiaxiangmudataList: [],
        visible: false,
        //索赔单列表开关
        suopeidanFlag: false,
        kaohedanFlag: false,
        visibleDrawer: false,
        collapse: false,
        infoId: null,
        printList: {
          rows: [{
          // dealerCode:'test001',//服务站号
          // dealerName:'吉利01',//服务站简称
          // seriesId:'星越',//车系
          // claimNo:'dh0000000001',//索赔单号
          // model:'车型',//车型
          // engineNo:'FDJ00000000000001',//发动机号
          // vin :'vin00000001',//vin码
          // evNo:'evNo00000001',//电池编号
          // prodDate:'2020-10-10 15:20:10',//生产日期
          // saleDate:'2020-10-10 15:20:10',//销售日期
          // mileage:'50000',//行驶里程
          // repairDate:'2020-10-10 15:20:10',//维修日期
          // type:'1',//索赔类型
          // faultAppear:'故障',//故障现象
          // dealerName:'服务站简称',//服务站简称
          // licenseNo:'渝AH2W73',//车牌号
          // codeUrl:'https:www.baidu.com',//二维码
          // partCode:'partCode0000001',//旧件代码
          // partsName:'partCodeName',//旧件名称
          // number:'1',//数量
          // supCode:'gxs00001',//供应商编码
          }],
        },
        flterValue: {}, //参数
        columns: this.creatHeader(),
        withholdColumns:this.creatWithholdHeader(),
        columns1: this.creatHeader1(),
        columns2:this.creatHeader2(),
        columns3:this.creatHeader3(),
        fetchParams: {}, //ipa 接口
        selectedRows: [], //选中服务站
        formList: this.createFormList(),
        editRow: {},
        saveData: null,
        upVisible:false,
        assessId:null
      };
    },
    async created() {
      // await this.chuqi1();
      //await this.chuqi();
      //await this.getDetailFun(this.rowData.settleId);
      await this.findBuyItems();
      await this.valueForTable3();
      //await this.valueForTable2();
    },
    methods: {

      sumAmount() {
        let sum = 0;

        //索赔金额
        this.$refs.table.$refs.pageTable.list.map(x => {
          sum += Number(x.sumAmount);
        });
        //考核金额
        this.$refs.table1.$refs.pageTable.list.map(x => {
          sum += Number(x.assessAmount);
        });

        // this.$refs.peijianhuanshangjiantable.$refs.pageTable.list.map(x => {
        //   sum += Number(x.cmsClaimPrice);
        // });

        this.formList.map(x => {
          if('settleAmount' == x.fieldName) {
            x.initialValue = sum;
          }
        });

      },
      formChangeHandle(val) {
        this.saveData = val;
      },
       async  batchExamineDetails() {
       //let res = await getClaimInfoByNo(this.rowData.settleNo);
       let arr = [];
       arr.push(this.printList.rows);
       this.printList.rows = arr;
      //  this.printList.rows.map((x) => {
      //   if (x.appId == "beijing") {
      //     x.logo = require('@/assets/img/beijing.png');
      //   } else {
      //     x.logo = require('@/assets/img/arcfox.png');
      //   }
      //   // url 转 二维码
      //   // this.getqrcode(x,x.codeUrl);
      // })
      this.$nextTick(() => {
        this.$refs.templateReturnDetails.EXCUTE_PRINT();
      });
    },
      //搜索获取数据
      changeHandle(val) {
        this.fetchParams = Object.assign({}, this.fetchParams, val);
      },
      //索赔单计算
      suopeidansave(val) {
        this.$refs.table.$refs.pageTable.EXECUTE_INSERT(val);
        let suopeijine = 0;
        this.$refs.table.$refs.pageTable.list.map(x => {
          suopeijine += Number(x.sumAmount);
        });
        this.formList.map(x => {
          if ('claimAmount' == x.fieldName) {
            x.initialValue = Number(suopeijine);
          }
        });
        this.suopeisum=suopeijine;
        this.sumAmount();
        this.suopeidanFlag = false;
        this.fetchParams = { ...this.fetchParams };
      },//考核单计算
      kaohedansave(val) {
        this.$refs.table1.$refs.pageTable.EXECUTE_INSERT(val);

        let kaohejine = 0;
        this.$refs.table1.$refs.pageTable.list.map(x => {
          kaohejine += Number(x.assessAmount);
        });
        this.formList.map(x => {
          if ('assessAmount' == x.fieldName) {
            x.initialValue = Number(kaohejine);
          }
        });
        this.kaohesum=kaohejine;
        this.sumAmount();
        this.kaohedanFlag = false;
        this.fetchParams = { ...this.fetchParams };
      },
      suopeidanclose() {
        this.suopeidanFlag = false;
      },
      suopeidanadd() {
        this.suopeidanFlag = true;
      },
      kaohedanclose() {
        this.kaohedanFlag = false;
      },
      kaohedanadd() {
        this.kaohedanFlag = true;
      },

      //选中
      selectChange(rows) {
        this.selectedRows = rows;
      },
      //保存
      async saveDialog() {
        this.$refs.formPanel.SUBMIT_FORM();

        let str = '';
        this.$refs.table.$refs.pageTable.list.map(x => {
          str += x.claimId + ',';
        });
        this.saveData.claimIds=str;

        let str1 = '';
        this.$refs.table1.$refs.pageTable.list.map(x => {
          str1 += x.assessId + ',';
        });
        this.saveData.assessIds=str1;

        for (let a in this.editRow) {
          if (this.saveData[a]) {
            this.editRow[a] = this.saveData[a];
          }
        }
        if (this.saveData !== null) {
          let res = await editSave(this.editRow);
          if (res.resultCode === 200) {
            this.closeDialog('refresh');
            return this.$notify.success({ title: '提示信息', message: '保存成功' });
            this.$emit('save');
          } else {
            this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
            this.closeDialog();
          }

        }else {
          return this.$notify.warning({ title: '提示信息', message: '有未校验通过的信息' });
        }

      },
      //给input框赋值
      async findBuyItems() {
      this.formList.map(x => {
        x.initialValue = this.rowData[x.fieldName];
      });
      //this.printList.rows = Object.assign(this.rowData,this.printList.rows);
       let res = await getClaimInfoByNo(this.rowData.settleNo);
       //为table赋值(索赔单)
        if (200 == res.resultCode) {
          let data = res.data;
          if (isFormEmpty(data)) {
            data = {};
          }
          // let records = data.claimList;
          // if (isFormEmpty(records)) {
          //   records = [];
          // }
          this.$refs.table.$refs.pageTable.EXECUTE_INSERT(data);
          //this.printList.rows.push({partClaim : data.partClaim})
          //this.printList.rows = Object.assign(data,this.printList.rows);
          this.$refs.table.$refs.pageTable.list.map((x) => {
            if(x.ifPrice == '是'){
              x.finalSumAmount =x.priceAmount;
            }
          });
        }

        let res2 = await getCloseAccountCount(this.rowData.settleNo);
       //为table赋值(结算信息)
        if (200 == res2.resultCode) {
          var values = res2.data;
          var detail = Object.assign({
            project: "索赔单数量"
          },values[0]);
          if (isFormEmpty(detail)) {
            detail = {};
          }
          /**
           * 索赔单分类数量汇总表格   填充参数渲染打印单据
           */
          let params = {
            normalClaim:detail.normalClaim, //正常索赔数量
            preClaim:detail.preClaim,  //配件索赔数量
            firstMaintenance:detail.firstMaintenance,  //首保索赔数量
            pdi:detail.pdi,  //pdi索赔数量
            serviceAction:detail.serviceAction,  //服务活动数量
            technologicalUpgrade:detail.technologicalUpgrade,  //技术升级
            partClaim:detail.partClaim,  //备件索赔数量
            dealerCode:this.rowData.dealerCode,  //服务站代码
            dealerName:this.rowData.dealerName,  //服务站名称
            settleNo:this.rowData.settleNo,  //结算单号
            settleDate:this.rowData.settleDate,  //结算日期
            settleAmount:this.rowData.settleAmount,  //结算金额
            };

          this.$refs.table2.$refs.pageTable.EXECUTE_INSERT(detail);
          this.printList.rows = Object.assign(params,this.printList.rows);
        }
    },

        //为table3赋值  费用明细
      async valueForTable3(){

      let res3 = await getCostCountByNo(this.rowData.settleNo);
       //为table赋值(结算信息)
        if (200 == res3.resultCode) {
          var values = res3.data;
          var detail = Object.assign({
            project: "金额(元)"
          },values[0]);
          if (isFormEmpty(detail)) {
            detail = {};
          }
          /**
           * 费用结算表格  填充费用参数渲染打印单据
           */
          let params = {
            labourAmount:detail.labourAmount, //工时费用
            helpAmount:detail.helpAmount,  //外出服务费
            addAmount:detail.addAmount,  //附加服务费
            firstMaintenanceCount:detail.firstMaintenanceCount,  //首保
            serviceCount:detail.serviceCount,  //服务活动
            upgradeCount:detail.upgradeCount,  //技术升级
            goodWillClaimCount:detail.goodWillClaimCount,  //善意索赔
            invoiceDate5:detail.invoiceDate5,  //旧件回运费用
            };
          //this.printList.rows.add({partClaim : detail.partClaim})
          this.$refs.table3.$refs.pageTable.EXECUTE_INSERT(detail);
          this.printList.rows = Object.assign(params,this.printList.rows);
        }
    },
      //取消操作
      closeDialog() {
        this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
      },
      editFile(row){
        this.upVisible = true;
        this.assessId = row.assessId;
      },
      chooseOrderNo() {
        this.visible = true;
      },
      add() {},
      createFormList() {
        return [
          {
            type: 'INPUT',
            label: '结算单号',
            fieldName: 'settleNo',
            disabled: true // 只读
          },
          {
            type: 'INPUT',
            label: '服务站代码',
            fieldName: 'dealerCode',
            disabled: true // 只读
          },
          {
            type: 'INPUT',
            label: '服务站名称',
            fieldName: 'dealerName',
            disabled: true // 只读
          },
          {
            type: 'INPUT',
            label: '结算单类型',
            fieldName: 'finalStatementType',
            disabled: true // 只读
          },
          {
            type: 'INPUT',
            label: '结算时间',
            fieldName: 'settleDate',
            disabled: true // 只读
          },
          {
            type: 'INPUT',
            label: '结算金额',
            fieldName: 'settleAmount',
            disabled: true // 只读
          },
        ];
      },
      creatHeader2() {
        return [
          // {
          //   title: '序号',
          //   dataIndex: 'index',
          //   width: 80,
          //   sorter: true,
          //   render: props => {
          //     return <span>{props.row.index + 1}</span>;
          //   }
          // },
          { title: '项目', dataIndex: 'project', sorter: true },
          { title: '正常索赔', dataIndex: 'normalClaim', sorter: true },
          { title: '售前索赔', dataIndex: 'preClaim', sorter: true },
          { title: '首保', dataIndex: 'firstMaintenance', sorter: true },
          { title: 'PDI', dataIndex: 'pdi', sorter: true },
          { title: '服务活动', dataIndex: 'serviceAction', sorter: true },
          { title: '技术升级', dataIndex: 'technologicalUpgrade', sorter: true },
          { title: '备件索赔', dataIndex: 'partClaim', sorter: true },
          { title: '善意索赔', dataIndex: 'kindnessClaim', sorter: true }
        ];
      },
      creatHeader3() {
        return [
          // {
          //   title: '序号',
          //   dataIndex: 'index',
          //   width: 80,
          //   sorter: true,
          //   render: props => {
          //     return <span>{props.row.index + 1}</span>;
          //   }
          // },
          { title: '项目', dataIndex: 'project', sorter: true },
          { title: '工时费', dataIndex: 'labourAmount', sorter: true },
          { title: '管理费', dataIndex: 'partAmount', sorter: true },
          { title: '外出服务费', dataIndex: 'helpAmount', sorter: true },
          { title: '附加费用', dataIndex: 'addAmount', sorter: true },
          { title: '首保费用', dataIndex: 'firstMaintenanceCount', sorter: true },
          { title: 'PDI费用', dataIndex: 'pdiCount', sorter: true },
          { title: '服务活动费用', dataIndex: 'serviceCount', sorter: true },
          { title: '技术升级费用', dataIndex: 'upgradeCount', sorter: true },
          { title: '善意索赔', dataIndex: 'goodWillClaimCount', sorter: true },
          { title: '旧件回运费用', dataIndex: 'invoiceDate5', sorter: true } //单独算
        ];
      },
      //抵扣单列表
      creatWithholdHeader() {
        return [
          {
            title: '序号',
            dataIndex: 'index',
            width: 80,
            sorter: true,
            render: props => {
              return <span>{props.row.index + 1}</span>;
            }
          },
          { title: '抵扣单号', dataIndex: 'claimNo', sorter: true },
          { title: 'VIN', dataIndex: 'vin', sorter: true },
          { title: '车型名称', dataIndex: 'modelName', sorter: true },
          { title: '抵扣类型', dataIndex: 'cmsClaimType', sorter: true,dictItems: this.createDictList('5510') },
          { title: '抵扣金额', dataIndex: 'finalSumAmount', sorter: true,precision:2 },
          { title: '工时费', dataIndex: 'labourAmount', sorter: true },
          { title: '备件费', dataIndex: 'partAmount', sorter: true },
          { title: '附加费用', dataIndex: 'addAmount', sorter: true },
          { title: '审核时间', dataIndex: 'checkAt', sorter: true },
          { title: '审核人', dataIndex: 'checkBy', sorter: true },
        ];
      },
      //索赔单列表
      creatHeader() {
        return [
          {
            title: '序号',
            dataIndex: 'index',
            width: 80,
            sorter: true,
            render: props => {
              return <span>{props.row.index + 1}</span>;
            }
          },
          { title: '索赔单号', dataIndex: 'claimNo', sorter: true },
          { title: 'VIN', dataIndex: 'vin', sorter: true },
          { title: '车型名称', dataIndex: 'modelName', sorter: true },
          { title: '索赔类型', dataIndex: 'cmsClaimType', sorter: true,dictItems: this.createDictList('5510') },
          { title: '索赔金额', dataIndex: 'finalSumAmount', sorter: true,precision:2 },
          { title: '工时费', dataIndex: 'labourAmount', sorter: true },
          { title: '备件费', dataIndex: 'partAmount', sorter: true },
          { title: '附加费用', dataIndex: 'addAmount', sorter: true },
          { title: '审核时间', dataIndex: 'checkAt', sorter: true },
          { title: '审核人', dataIndex: 'checkBy', sorter: true },
        ];
      },
      //考核单列表
      creatHeader1() {
        return [
          {
            title: '序号',
            dataIndex: 'index',
            width: 80,
            sorter: true,
            render: props => {
              return <span>{props.row.index + 1}</span>;
            }
          },
          {
            title: '操作',
            dataIndex: 'column-action',
            width: 90,
            render: props => {
              return (
                <div style="text-align:center">
                  <multiuse-button type="text" onClick={() => this.editFile(props.row)} >
                    查看附件
                  </multiuse-button>
                </div>
              );
            }
          },
          { title: '考核单号', dataIndex: 'assessNo', sorter: true },
          { title: '考核类型', dataIndex: 'assessTypeStr', sorter: true },
          { title: '考核金额', dataIndex: 'assessAmount', sorter: true },
          { title: '考核原因', dataIndex: 'assessReason', sorter: true },
          { title: '考核日期', dataIndex: 'assessDate', sorter: true },
          { title: '考核人', dataIndex: 'assessByStr', sorter: true },
          { title: '主键', dataIndex: 'assessId', hidden: true }
        ];
      }
    }
  };
</script>
<style lang="less" scoped></style>
